<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>电商后台管理页面</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '65px' : '200px'">
        <div class="toggle-btn" @click="toggleCollapse">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse-transition="false"
          :collapse="isCollapse"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <!-- 模板区域 -->
            <template slot="title">
              <!-- 通过循环得到id值获取对应属性并动态绑定 -->
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index=" '/' +subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' +subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      // 左侧菜单栏数据
      menuList: [],
      // 一级菜单的字体图标属性
      iconObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 控制菜单伸缩
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  // 生命周期 函数
  created() {
    this.getMenuList()
    // 获取被激活的链接地址，高亮选项
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 清除token
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取菜单数据，并渲染左侧列表
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 将数据保存到当前组件的menulist中
      this.menuList = res.data
    },
    // 菜单展开缩小
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  > div {
    display: flex;
    align-items: center;
    > span {
      padding-left: 10px;
      font-size: 20px;
      color: #fff;
    }
  }
}
.el-aside {
  background: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-menu {
  border-right: none;
}
.iconfont {
  padding-right: 10px;
}
.toggle-btn {
  background-color: #4a5064;
  font-size: 16px;
  line-height: 40px;
  text-align: center;
  letter-spacing: 0.2em;
  color: #fff;
  cursor: pointer;
}
</style>
